package com.ssy.lingxi.platform.template.service.controller.web;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.platform.template.api.model.dto.inner.MemberPurchaseInnerDTO;
import com.ssy.lingxi.platform.template.service.entity.door.MemberPurchase;
import com.ssy.lingxi.platform.template.service.model.dto.common.CollectDTO;
import com.ssy.lingxi.platform.template.service.model.dto.common.IdDTO;
import com.ssy.lingxi.platform.template.service.model.dto.common.MemberIdAndRoleIdDTO;
import com.ssy.lingxi.platform.template.service.model.dto.web.SavePurchaseDTO;
import com.ssy.lingxi.platform.template.service.model.qo.MemberPurchaseQO;
import com.ssy.lingxi.platform.template.service.model.vo.web.MemberPurchaseListVO;
import com.ssy.lingxi.platform.template.service.model.vo.web.MemberPurchaseMainVO;
import com.ssy.lingxi.platform.template.service.service.web.MemberPurchaseWebService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author fhj
 * @version 2.0.0
 * @menu web - 会员采购门户
 * @date 2021/04/14
 */
@Api(tags = "web - 会员采购门户")
@RestController
@RequestMapping("/template/web/memberPurchaseWeb")
public class MemberPurchaseWebController extends BaseController {

    @Resource
    private MemberPurchaseWebService memberPurchaseWebService;

    @ApiOperation(value = "保存当前登录会员的采购门户")
    @PostMapping("/saveCurrMemberPurchase")
    public Wrapper<Void> saveCurrMemberPurchase(@RequestBody @Valid SavePurchaseDTO dto) {
        return memberPurchaseWebService.saveCurrMemberPurchase(dto, getSysUser());
    }

    @ApiOperation(value = "获取当前登录会员的采购门户")
    @GetMapping("/findCurrMemberPurchase")
    public Wrapper<MemberPurchase> findCurrMemberPurchase() {
        return memberPurchaseWebService.findCurrMemberPurchase(getSysUser());
    }

    @ApiOperation(value = "采购门户列表")
    @GetMapping("/memberPurchaseList")
    public Wrapper<PageData<MemberPurchaseListVO>> memberPurchaseList(@Valid MemberPurchaseQO qo, HttpServletRequest request) throws UnsupportedEncodingException {
        String memberName = request.getParameter("memberName");
        if (StringUtils.isNotBlank(memberName)) {
            memberName = java.net.URLDecoder.decode(memberName, "UTF-8");
            qo.setMemberName(memberName);
        }
        return memberPurchaseWebService.memberPurchaseList(qo);
    }

    @ApiOperation(value = "活跃采购商")
    @GetMapping("/activeMemberPurchase")
    public Wrapper<List<MemberPurchase>> activeMemberPurchase() {
        return memberPurchaseWebService.activeMemberPurchase();
    }

    @ApiOperation(value = "最新加入")
    @GetMapping("/newAddMemberPurchase")
    public Wrapper<List<MemberPurchase>> newAddMemberPurchase() {
        return memberPurchaseWebService.newAddMemberPurchase();
    }

    @ApiOperation(value = "采购门户主页")
    @GetMapping("/memberPurchaseMain")
    public Wrapper<MemberPurchaseMainVO> memberPurchaseMain(@Valid IdDTO dto, Long memberId, Long roleId) {
        return memberPurchaseWebService.memberPurchaseMain(dto, memberId, roleId, isLogin() ? getSysUser() : null);
    }

    @ApiOperation(value = "根据会员ID和角色ID获取采购门户")
    @GetMapping("/findByMemberIdAndRoleId")
    public Wrapper<MemberPurchaseMainVO> findByMemberIdAndRoleId(@Valid MemberIdAndRoleIdDTO dto) {
        return memberPurchaseWebService.findByMemberIdAndRoleId(dto);
    }

    @ApiOperation(value = "收藏/取消收藏")
    @PostMapping(value = "/collect")
    public Wrapper<Void> collect(@RequestBody @Validated CollectDTO dto) {
        return memberPurchaseWebService.collect(dto, getSysUser());
    }

    @ApiOperation(value = "收藏列表")
    @GetMapping(value = "/collectList")
    public Wrapper<PageData<MemberPurchase>> collectList(PageVO dto) {
        return memberPurchaseWebService.collectList(dto, getSysUser());
    }

    /**
     * 判断是否创建采购门户
     *
     * @param dto 请求参数
     * @return 操作结果
     */
    @PostMapping("/exitMemberPurchase")
    public Wrapper<Boolean> exitMemberPurchase(@RequestBody @Valid MemberPurchaseInnerDTO dto) {
        return memberPurchaseWebService.exitMemberPurchase(dto);
    }
}
